﻿namespace Paw.Phone.Pages
{
    using System;
    using System.Windows;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using Paw.Phone.ViewModel;

    public partial class LoginPage : PhoneApplicationPage
    {
        public LoginPage()
        {
            this.InitializeComponent();

            this.ViewModel = new LoginPageViewModel();
        }

        public LoginPageViewModel ViewModel
        {
            get { return this.DataContext as LoginPageViewModel; }
            set { this.DataContext = value; }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            this.ViewModel.UserName = App.CloudClientFactory.UserName ?? this.ViewModel.UserName;
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            if (e.NavigationMode == System.Windows.Navigation.NavigationMode.Back)
            {
                if (!GetApplicationState<bool>("UserLoggedIn"))
                {
                    // Save a flag to notify that the user wants to close the application.
                    SetApplicationState("UserGoesBack", true);
                }
            }
        }

        private static T GetApplicationState<T>(string key)
        {
            if (!PhoneApplicationService.Current.State.ContainsKey(key))
            {
                return default(T);
            }

            return (T)PhoneApplicationService.Current.State[key];
        }

        private static void SetApplicationState(string key, object value)
        {
            if (PhoneApplicationService.Current.State.ContainsKey(key))
            {
                PhoneApplicationService.Current.State.Remove(key);
            }

            PhoneApplicationService.Current.State.Add(key, value);
        }

        private void OnLoginButtonClick(object sender, RoutedEventArgs e)
        {
            if (this.ViewModel.IsLoginEnabled)
            {
                this.ViewModel.Login(
                    () =>
                    {
                        // Save the username of the last user logged in.
                        App.CloudClientFactory.SetUserName(this.ViewModel.UserName, this.ViewModel.RememberMe);

                        // Save a flag to notify that the user is already logged in.
                        SetApplicationState("UserLoggedIn", true);

                        this.NavigationService.GoBack();
                    },
                    msg =>
                    {
                        MessageBox.Show(msg, "Log in Error", MessageBoxButton.OK);
                    });
            }
            else
            {
                MessageBox.Show("Please fill out all the information in the login page.", "Login incomplete", MessageBoxButton.OK);
            }
        }

        private void OnTextBoxKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                if (sender == this.UserNameTextBox)
                {
                    this.PasswordBox.Focus();
                }
                else
                {
                    this.LoginButton.Focus();
                }
            }
        }

        private void OnRegisterButtonClick(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Pages/RegisterPage.xaml", UriKind.Relative));
        }
    }
}